package com.gyx.metaldesigndemo.recycle;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyx.metaldesigndemo.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/11/15.
 */

public class RecyclerViewStaggeredAdapter extends RecyclerView.Adapter<RecyclerViewStaggeredAdapter.MyHolder> {

    private Context context;

    private ArrayList<String> list;

    public RecyclerViewStaggeredAdapter(Context context){
        this.context=context;

        list=new ArrayList<>();
        for (int i=0 ; i<50 ;i++){
            list.add("item-"+i);
        }
    }


    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new MyHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_view,parent,false));
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
          holder.textView.setText(list.get(position));


        ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
        if(position%7==0||position%3==0){

            if(layoutParams instanceof StaggeredGridLayoutManager.LayoutParams){
                StaggeredGridLayoutManager.LayoutParams lp= (StaggeredGridLayoutManager.LayoutParams) layoutParams;
                lp.setFullSpan(true);
                holder.itemView.setLayoutParams(layoutParams);
            }
        }else{

            if(layoutParams instanceof StaggeredGridLayoutManager.LayoutParams){
                StaggeredGridLayoutManager.LayoutParams lp= (StaggeredGridLayoutManager.LayoutParams) layoutParams;
                lp.setFullSpan(false);
                holder.itemView.setLayoutParams(layoutParams);
            }
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public  static class MyHolder extends RecyclerView.ViewHolder{

        TextView textView;
        public MyHolder(View itemView) {
            super(itemView);
            textView= (TextView) itemView.findViewById(R.id.tv1);
        }
    }
}
